// pages/feedback/feedback.js

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    PROBLEMNAME: '', //标题
    PROBLEMNOTE: '', //内容
    CUSTOMER_TEL: '', //手机号
    avatar: '', //图片
    openid: '',
    //上传图片
    imgs: [],
    count: 1,
    flag: true, //设置节流
  },

  back() {
    wx.switchTab({
      url: '../../pages/personage/personage',
    })
  },


  //上传图片
  activation: function (e) {
    console.log(111);
    switch (this.data.imgs.length) {
      case 0:
        this.data.count = 2
        break
    }
    var that = this
    wx.chooseImage({
      count: that.data.count, // 默认3
      sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res);
        console.log(res.tempFilePaths);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          //限制上传图片大小（4M）
          if (res.tempFiles[i].size > 4500000) {
            wx.showModal({
              content: '上传图片不能大于4M!',
              showCancel: false,
            });
            return false;
          } else {
            wx.uploadFile({
              url: 'https://jiagaojie.xajiarong.net/youchuhuiqin/Test/API_1169_uploadFileCOS',
              method: 'POST',
              name: "FIMG",
              filePath: tempFilePaths[i],
              header: {
                'Content-Type': 'multipart/form-data'
              },
              formData: {
                KEY: 'xianjgjKey9999',
                OPEN_ID: that.data.openid, //用户OPEN_ID
                session_key: that.data.session_key,
                OPEN_UUID: that.data.jiami
              },
              success: function (res) {
                console.log(JSON.parse(res.data));
                let img_xi = JSON.parse(res.data)
                if (img_xi.result == 'success') {
                  wx.showToast({
                    title: "上传成功",
                    icon: "none",
                    duration: 1500
                  })
                  setTimeout(() => {
                    that.setData({
                      avatar: img_xi.newFile
                    })
                    console.log(that.data);
                    console.log(that.data.avatar);
                  }, 1000);


                }
              },
              fail: function (err) {
                console.log(err);
                wx.showToast({
                  title: "上传失败",
                  icon: "none",
                  duration: 2000
                })
              },
              complete: function (result) {
                console.log(result.errMsg)
              }
            })
          }
        }
      }
    })
  },




  // 提交反馈
  submit() {


    if (this.data.flag) {
      this.data.flag = false



    if (this.data.PROBLEMNOTE == '' || this.data.PROBLEMNAME == '' ||this.data.avatar == ''  ) {
      wx.showToast({
        title: "请完善反馈或内容",
        icon: "none",
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.interface+'/newVersion/API_2055_problemFeedback',
        method: 'POST',
        //向后端发送的数据
        data: {
          PROBLEMNOTE: this.data.PROBLEMNOTE,
          PROBLEMNAME: this.data.PROBLEMNAME,
          PROBLEMPHONE: this.data.CUSTOMER_TEL,
          PROBLEMOPENID: this.data.openid, //将code发送到后台服务器。
          PROBLEMIMG: this.data.avatar,
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
        },
        success: res => {
          console.log(res.data.result);
          if (res.data.result == 'success') {

            wx.showToast({
              title: '问题已反馈！',
              icon: 'none',
              duration: 3000
            })
          }

        }
      })
    }

    setTimeout(() => {
      this.data.flag = true
    }, 5000)

  }
  },


  // 判断是否授权
  scanCod() {
    var that = this
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2007_getPhoneByOpenId',
      method: 'POST',
      //向后端发送的数据
      data: {
   
        OPEN_ID: this.data.openid, //用户OPEN_ID
        session_key: this.data.session_key,
        OPEN_UUID: this.data.jiami //将code发送到后台服务器。
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
      },
      success: res => {
        console.log(res);
        console.log(res.data.data.CUSTOMER_TEL);
        if (res.data.result != 'success') {
          wx.showToast({
            title: '请获取手机号',
            duration: 1000
          })
          that.setData({
            Getphone: false,
          })
        } else {
          that.setData({

            CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,

          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.setData({
  
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      openid: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_TEL: app.globalData.CUSTOMER_TEL,
    });
    console.log();
    if (this.data.openid != '') {
      this.scanCod()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})